package com.baizhi.action.front;

import com.baizhi.entity.*;
import com.baizhi.service.AddressService;
import com.baizhi.service.BookService;
import com.baizhi.service.OrderItemService;
import com.baizhi.service.OrderService;
import com.baizhi.service.impl.AddressServiceImpl;
import com.baizhi.service.impl.BookServiceImpl;
import com.baizhi.service.impl.OrderItemServiceImpl;
import com.baizhi.service.impl.OrderServiceImpl;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

public class OrderAction {
    private Address address;

    private Order order;

    private String orderId;
    private Double totalPrice;

    public Order getOrder() {
        return order;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public Double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(Double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String addOrder(){
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        AddressService addressService=new AddressServiceImpl();
        OrderService orderService=new OrderServiceImpl();
        OrderItemService orderItemService=new OrderItemServiceImpl();
        User user = (User) session.getAttribute("user");

        //从session中获取购物车，然后转换为订单
        Cart cart = (Cart) session.getAttribute("cart");

        if (cart==null){
            return "addOrderError";
        }else {
            //表示是新建的地址
            if (address.getAddressId().isEmpty()||address.getAddressId()==null){
                String uuid1= UUID.randomUUID().toString();
                address.setAddressId(uuid1);
                address.setUserId(user.getUserId());
                addressService.addAddress(address);
            }

            Order order = new Order();
            String uuid2= UUID.randomUUID().toString();
            order.setOrderId(uuid2);
            order.setUserId(user.getUserId());
            order.setCreateDate(new Date());
            order.setOrderStatus("未支付");
            order.setAddress(address);
            order.setTotal(cart.getTotalPrice());
            orderService.addOrder(order);

            Map<String, CartItem> items = cart.getItems();

            Set<Map.Entry<String, CartItem>> entries = items.entrySet();

            OrderItem orderItem=new OrderItem();
            for (Map.Entry<String, CartItem> entry : entries) {
                String uuid3= UUID.randomUUID().toString();
                orderItem.setOrderItemId(uuid3);
                orderItem.setBookId(entry.getKey());
                orderItem.setCount(entry.getValue().getCount());
                orderItem.setCreateDate(new Date());
                orderItem.setOrderId(order.getOrderId());
                orderItemService.addOrderItem(orderItem);
            }
            totalPrice=cart.getTotalPrice();
            orderId=order.getOrderId();
            session.removeAttribute("cart");
            return "addOrderSuccess";
        }

    }

    public String updateOrder(){
        OrderService orderService=new OrderServiceImpl();
        OrderItemService orderItemService=new OrderItemServiceImpl();
        BookService bookService=new BookServiceImpl();
        order = orderService.selectOrderById(orderId);
        order.setOrderStatus("已支付");
        orderService.updateOrderPayStatus(order);

        List<OrderItem> orderItems = orderItemService.selectOrderForOrderItemsById(orderId);

        for (OrderItem orderItem : orderItems) {
            Book book = orderItem.getBook();
            book.setSale(book.getSale()+orderItem.getCount());
            bookService.updateBook(book);
        }
        totalPrice=order.getTotal();
        return "updateOrderPayStatusSuccess";
    }
}
